package de.karnik.dokobu.controllers;

import java.util.List;

import javax.jdo.Transaction;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import de.karnik.dokobu.persistence.DoKoBuUser;
import de.karnik.dokobu.persistence.DoKoBuUserDao;
import de.karnik.dokobu.persistence.Ingredient;
import de.karnik.dokobu.persistence.Recipe;
import de.karnik.dokobu.persistence.RecipeCategory;
import de.karnik.dokobu.persistence.RecipeDao;
import de.karnik.dokobu.persistence.RecipeIngredient;

@Controller
@RequestMapping(value="/index")
public class IndexController extends BaseController {

	 @RequestMapping( method = RequestMethod.GET )
	 public String index( HttpServletRequest request, ModelMap model ) {
		 this.init();
		 
		 this.getBaseAttributes().put( "title", "Index" );
		 DoKoBuUserDao ud = DoKoBuUserDao.getInstance();
		 Transaction tx = ud.getPersistenceManager().currentTransaction();
		 
		 tx.begin();
		 DoKoBuUser a = new DoKoBuUser( "markus.karnik@googlemail.com", "Markus Karnik", DoKoBuUser.ROLE_ADMIN );
		 ud.save( a );
		 tx.commit();
		 
		 tx.begin();
		 DoKoBuUser b = new DoKoBuUser( "dominika.karnik@googlemail.com", "Dominika Karnik", DoKoBuUser.ROLE_USER );
		 ud.save( b );
		 tx.commit();

		 tx.begin();
		 DoKoBuUser c = new DoKoBuUser( "m.karnik@googlemail.com", "Markus Karnik", DoKoBuUser.ROLE_USER );
		 ud.save( c );
		 tx.commit();
		 
		 List<DoKoBuUser> users = ud.getAll();
		 
		 StringBuilder sb = new StringBuilder();
		 
		 for( int i = 0; i < users.size(); i++ ) {
			 sb.append( "UserName: " ).append( users.get( i ).getDisplayName() ).append( "<br />")
			 	.append( "UserID: " ).append( users.get( i ).getGoogleID() ).append( "<br />" );
		 }
		 
		 /*
		 RecipeDao rd = RecipeDao.getInstance();
		 tx.begin();
		 Recipe recipe = new Recipe();
		 recipe.setName( "testrezept" );
		 recipe.setCalories( 1000 );
		 recipe.setCookingTime( 100 );
		 recipe.setImage( null );
		 recipe.setInstruction( "asdasasasd" );
		 recipe.setSpecialMeal( true );
		 recipe.setType( "" );
		 recipe.setCreatedBy( a );
		 recipe.setLastmod( a );
		 
		 RecipeCategory cat = new RecipeCategory();
		 cat.setName( "testcat" );
		 recipe.setCategory( cat );
		 
		 
		 RecipeIngredient ri = new RecipeIngredient();
		 ri.setUnit( "g" );
		 ri.setValue( 10f );
		 Ingredient i = new Ingredient();
		 i.setName( "test" );
		 ri.setIngredient( i );
		 
		 recipe.addRecipeIngredient( ri );
		 
		 
		 rd.save( recipe );
		 tx.commit();
		 */
		 
		 this.getBaseAttributes().put( "content", sb.toString() );
		 model.mergeAttributes( this.getBaseAttributes() );
		 
		 
		 return "index/index";
	 }
	
}
